﻿///
///层级列表数据处理器，目前仅限支持二级分类
///
(function ($) {
    $.fn.hierarchyListviewHandler = function () {
        var $this = this;
        if ($this.data('hierarchyListviewHandler')) {
            return $this.data('hierarchyListviewHandler');
        }

        $this.data('hierarchyListviewHandler', this);
        //初始化
        $this.initialize = function (options) {
            var defoption = {
                //loaderBtn: '', //加载数据按钮（点击加载下一页）
                emptyHtml: '无记录', //无记录时显示此html
                itemTemplate: '', //项模板
                itemHandler: function (data, itemTemplate, container) {//数据各项处理
                    return itemTemplate.format(data);
                },

                command: 'Pub.LoadCategory',
                parameter: {}
            };
            $this.options = $.extend({}, defoption, options);

            //   $this.options.loaderBtn.click(function () {
            //       $this.nextPage();
            //   });

            //隐藏加载按钮
            //   $this.options.loaderBtn.hide();

            //清空现有数据
            $this.empty();
        }

        //加载下一页
        $this.queryData = function () {
            var param = {};
            //调用接口
            $this.executeCommand({ successCallBack: function (result) {
                //$this.options.beforeHandleResult(result, $this);
                // if (totalCount == 0) {
                //     $this.append($this.options.emptyHtml);
                // }

                // if ($this.options.pageSize * $this.options.pageIndex < totalCount) {
                //     $this.options.loaderBtn.show();
                // } else {
                //     $this.options.loaderBtn.hide();
                // }

                // $this.options.totalCount = totalCount;
                // $this.options.pageIndex += 1;
                //遍历所有属性，找出array的属性作为列表数据
                var html = ''; // '<h2>所有分类</h2>';
                html += '<div data-role="collapsible-set">';
                var categoryList = result.categoryList;
                if (categoryList.length == 0) {
                    html = '<h2>暂没有分类信息</h2>';
                }
                else {
                    html = '';// '<h2>所有分类</h2>';
                    html += '<div data-role="collapsible-set">';
                    for (var i = 0; i < categoryList.length; i++) {
                        var category = categoryList[i];
                        html += '<div data-role="collapsible" data-iconpos="right" data-inset="false" data-collapsed="false" data-theme="e">';
                        html += '<h4> ' + category.CategoryName + '</h4>';
                        var childCategoryList = category.ChildCategoryList;
                        if (childCategoryList.length > 0) {
                            html += '<ul data-role="listview">';
                            for (var j = 0; j < childCategoryList.length; j++) {
                                var childCategory = childCategoryList[j];
                                html += '<li><a data-ajax="false" href="/wap/shop/ProductList.aspx?categoryId=' + childCategory.CategoryId + '">' + childCategory.CategoryName + '</a></li>'
                            }
                            html += '</ul>';
                        }
                        html += '</div>';
                    }
                    html += ' </div> ';
                }
                $this.append(html);
                $this.trigger("create");
            }


            }, $this.options.command, param);
        }

        return $this;
    }
})(jQuery);